package com.flash.dailyreport.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flash.dailyreport.entity.HousepriceEntity;
import com.flash.dailyreport.util.HttpClientUtils;
import com.flash.dailyreport.util.HttpTextResponse;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author sunyiming
 * @date 2018/8/17
 */
@Service
public class HousepriceService {

    HttpClientUtils client = HttpClientUtils.getInstance();

    public List<HousepriceEntity> listAll(){
        HttpTextResponse httpTextResponse = client.doHttpGet("http://www.creprice.cn/onemap/area.html", null);
        JSONObject responseJson = JSONObject.parseObject(httpTextResponse.getResponseBody());
        JSONArray features = responseJson.getJSONObject("map_data").getJSONArray("features");

        List<HousepriceEntity> houseprices = new ArrayList<>();
        for (Object feature : features) {
            HousepriceEntity entity = new HousepriceEntity();
            JSONObject properties = ((JSONObject) feature).getJSONObject("properties");
            entity.setCityname(properties.getString("cityName"));
            entity.setPrice(properties.getDouble("data"));
            entity.setSupplyCount(properties.getInteger("count"));
            houseprices.add(entity);
        }

        return houseprices;

    }

}
